package com.ucap.weking.guangan.fragments.liveOnlineFrgmentContent;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.adapter.ListSpeakAdapter;
import com.ucap.weking.guangan.base.BaseFragment;
import com.ucap.weking.guangan.bean.ListLIveReplyBean;
import com.ucap.weking.guangan.dataUri.DataUri;
import com.ucap.weking.guangan.dialog.PersonlDialog;
import com.ucap.weking.guangan.httpTools.HttpCallRequest;
import com.ucap.weking.guangan.test.TestData;
import com.ucap.weking.guangan.tools.PrefUtils;
import com.ucap.weking.guangan.views.MyToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by weking on 2017/11/13.
 * 访谈直播，网友心声
 */

public class FragmentContent1 extends BaseFragment {
    @InjectView(R.id.lv_content)
    ListView lvContent;
    @InjectView(R.id.et_exit)
    EditText etMsgText;
    @InjectView(R.id.btn_send)
    Button btnSend;
    @InjectView(R.id.iv_personal)
    ImageView ivPerson;
    String uri;
    private ArrayList<ListLIveReplyBean.RowsBean> dataList;
    private ListSpeakAdapter adapter;

    @Override
    public void setData() {
        Log.i("网友心声", "网友心声uri: " + uri);
        dataList = new ArrayList<>();
        adapter = new ListSpeakAdapter(dataList);
        lvContent.setAdapter(adapter);
        getData();
        intiListener();
        handler.sendEmptyMessage(0);
    }

    @Override
    public void intiView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.live_online_fragment_1, null);
        flContent.addView(view);
        ButterKnife.inject(this, view);
    }

    public void intiListener() {
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String personl_name = PrefUtils.getString("personl_name", "", getContext());
                String personl_phone = PrefUtils.getString("personl_phone", "", getContext());
                if (!TextUtils.isEmpty(personl_name) && !TextUtils.isEmpty(personl_phone)) {
                    if (!TextUtils.isEmpty(etMsgText.getText().toString())) {
                        uploadData(personl_name, personl_phone, etMsgText.getText().toString());
                    }
                } else {
                    PersonlDialog dialog = new PersonlDialog(getContext());
                    dialog.show();
                }
//
            }
        });
        ivPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonlDialog dialog = new PersonlDialog(getActivity());
                dialog.show();
            }
        });

    }

    public FragmentContent1 setDataUri(String uri) {
        this.uri = uri;
        return this;
    }

    public void uploadData(String name, String phone, String content) {
        HttpCallRequest.callRequest(DataUri.getLIVE_ONLINE_SPEAK_UPLOAD(name, phone, content), new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                Log.i("网友心声提问状态", "onRequest: " + body);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (TextUtils.indexOf(body, "true") != -1) {
                            etMsgText.setText("");
                        } else {
                            etMsgText.setText("");
                            MyToast.showTrue(getContext(), "提问失败请重试！");
                        }
                    }
                });
            }

            @Override
            public void onErr(Exception e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MyToast.showTrue(getContext(), "提问失败请重试！");
                    }
                });

            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            getData();
            handler.sendEmptyMessageDelayed(0, 5000);
        }
    };

    public void getData() {
        HttpCallRequest.callRequest(DataUri.getLIVE_ONLINE_SPEAK(), new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!TextUtils.isEmpty(body)) {
                                dataList.clear();
                                Gson gson = new Gson();
                                ListLIveReplyBean listLIveReplyBean = gson.fromJson(body, ListLIveReplyBean.class);
                                dataList.addAll(listLIveReplyBean.getRows());
                                adapter.notifyDataSetChanged();
                                lvContent.setSelection(dataList.size() - 1);
                            }
                        } catch (Exception e) {

                        }

                    }
                });

            }

            @Override
            public void onErr(Exception e) {

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeMessages(0);
    }
}
